<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * MyIndoCMS
 *
 * MyIndoCMS is Product for PT MyIndo Cyber Media
 *
 * @package		MyIndoCMS
 * @author		PT MyIndo Cyber Media
 * @copyright	Copyright (c) 2007, PT MyIndo Cyber Media
 * @license     http://www.myindo.co.id/license.html
 * @link        http://www.myindo.co.id
 * @since       Version 1.0
 * @filesource
 */

// ------------------------------------------------------------------------

/**
 * MyIndoCMS Template Helpers
 *
 * @package		MyIndoCMS
 * @subpackage	Helpers
 * @category	Helpers
 * @author      PT MyIndo Cyber Media
 * @link        http://www.myindo.co.id/cms/helpers/mytemplate_helper.html
 */

// ------------------------------------------------------------------------

function mydatetime($timestamp)
{
    $timestamp = strtotime($timestamp);
    return date("d-m-Y H:i:s", $timestamp);
}

function mydatetimeindo($timestamp)
{
    $timestamp = strtotime($timestamp);
    return date("d-M-Y H:i:s", $timestamp);
}

function mydate($timestamp)
{
	$timestamp = strtotime($timestamp);
    return date("d-M-Y", $timestamp);
}

function mytime($timestamp)
{
	$timestamp = strtotime($timestamp);
    return date("H:i:s", $timestamp);
}

function mymktime($year,$month,$day,$hour=0,$minute=0,$second=0)
{
    if (! checkdate($month,$day,$year)) return now();
    return mktime($hour,$minute,$second,$month,$day,$year);
}


function myGToJD($year,$month,$day)
{
    $t = GregorianToJD($month,$day,$year);
    $n = GregorianToJD(date('m'),date('d'),date('Y'));
    $result = $n - $t;
    if ($result < 0)
        $result *= -1;
    return $result;
}

function SQLTimeToMKTime($string) {
        $string = trim($string);
        if(empty($string)) {
                $time = time();
        } elseif (preg_match('/^\d{14}$/', $string)) {
                // it is mysql timestamp format of YYYYMMDDHHMMSS?
                $time = mktime(substr($string, 8, 2),substr($string, 10, 2),substr($string, 12, 2),
                                substr($string, 4, 2),substr($string, 6, 2),substr($string, 0, 4));
        } elseif (is_numeric($string)) {
                // it is a numeric string, we handle it as timestamp
                $time = (int)$string;
        } else {
                // strtotime should handle it
                $time = strtotime($string);
                if ($time == -1 || $time === false) {
                        // strtotime() was not able to parse $string, use "now":
                        $time = time();
                }
        }
        return $time;
}

function SQLTimeToString($string,$format="%Y")
{
    return strftime($format, SQLTimeToMKTime($string));
}




function duration($time)
{
    $now = now();
    $secs = $now - $time;
    $vals = array(
                'd' => $secs / 86400 % 7,
                'h' => $secs / 3600 % 24,
                'm' => $secs / 60 % 60,
                's' => $secs % 60
            );
     
    $ret = array();
     
    $added = false;
    foreach ($vals as $k => $v) {
    if ($v > 0 || $added) {
    $added = true;
    $ret[] = $v . $k;
    }
    }
     
    return join(' ', $ret);
} 

function monthName($kode_mounth){
		
		if (!$kode_mounth) return "";
		
		$mounth=array(
						"01"=>"Januari",
						"02"=>"Pebruari",
						"03"=>"Maret",
						"04"=>"April",
						"05"=>"Mei",
						"06"=>"Juni",
						"07"=>"Juli",
						"08"=>"Agustus",
						"09"=>"September",
						"10"=>"Oktober",
						"11"=>"November",
						"12"=>"Desember",
						
						"1"=>"Januari",
						"2"=>"Februari",
						"3"=>"Maret",
						"4"=>"April",
						"5"=>"Mei",
						"6"=>"Juni",
						"7"=>"Juli",
						"8"=>"Agustus",
						"9"=>"September", 
						);
						
		$res = $mounth[$kode_mounth];
						
		return $res;
}

function getUsia($time = '', $now = '') {
	if(!$time)
		return 0;
		
	$CI =& get_instance();
	$CI->lang->load('date');

	$time = strtotime($time);
	$now = time();
		
	$usia = '';
	if( $time > 0 ) {
		$ageList = timespan($time, $now);
		$age = explode(',', $ageList);
		$usia = isset($age[0]) ? $age[0] : '';
	}
	
	return $usia;
}
